<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Giỏ hàng</title>
</head>

<body>
<?php
				@session_start();
				if(isset($_GET["themgiohang"]))
				{
					$id=$_GET["themgiohang"];
					if(isset($_SESSION['giohang']) && is_array($_SESSION['giohang']))
					{
						$count= count($_SESSION['giohang']);
						$flag=false;
						for ($i = 0; $i < $count; $i++)
						{ 
						if($_SESSION['giohang'][$i]["id"] == $id){
							$_SESSION['giohang'][$i]["soluong"] +=1;
									$flag=true;
									break;
							}
							}
						if($flag == false)
						{$_SESSION['giohang'][$count]["id"]=$id;
							$_SESSION['giohang'][$count]["soluong"]=1;
							}
						}else {
							$_SESSION['giohang']=array();
							$_SESSION['giohang'][0]["id"]=$id;
							$_SESSION['giohang'][0]["soluong"]=1;
							echo "<script> comfirm('Please enter some text!');</script>";

							}
						header("Location: cts.php?id=".$id."");
						$sl = $_SESSION['giohang'][0]["soluong"];
						echo "yyyyyyyyyy".$sl;
				}

?>
<?php 
	require ('admin/models/m_sach.php'); 
	require ('admin/models/sharefunction.php'); 
	include ('login.php');
	require ('admin/models/insert_hoadon.php');
	dbconnect(); 
	
		
		if(isset($_POST["dh"]))
		{
			$_SESSION["ttt"]=$_POST['thongtinthem'];
			if(isset($_SESSION['giohang']))
			{ 
			if(isset($_SESSION['ID_Nguoi_dung']))
				{
					$tongtien=0;
					$giavon=0;
				for($i=0; $i<count($_SESSION['giohang']); $i++){
					if ($_SESSION['giohang'][$i] == NULL) continue;
					$stt=$i+1;
					$query = mysql_query("select * from sach where ID=".$_SESSION['giohang'][$i]['id']);
					$row = mysql_fetch_array($query);
					$query2=  mysql_query("SELECT * FROM gia_ban WHERE (ID_Sach='".$_SESSION['giohang'][$i]['id']."' and Thoi_gian<now()) order by Thoi_gian DESC limit 1");
					$row2 = mysql_fetch_array($query2);
					$tongtien=$tongtien + ($row2['Gia_ban'] * $_SESSION['giohang'][$i]['soluong']);
					$thanhtien= $row2['Gia_ban'] * $_SESSION['giohang'][$i]['soluong'];
					$query3 = mysql_query("SELECT * FROM Chi_tiet_nhap WHERE (ID_Sach='".$_SESSION['giohang'][$i]['id']."') order by ID DESC limit 1");
					$row3 = mysql_fetch_array($query3);
					$dongianhap=$row3['Gia_nhap'];
					$gia_nhap = $dongianhap * $_SESSION['giohang'][$i]['soluong'];
					$giavon +=$gia_nhap;
				}
				$laisuat=$tongtien-$giavon;
				insert_hoadon($_SESSION['ID_Nguoi_dung'],$tongtien,$giavon,$laisuat, $_SESSION["ttt"]);

				if(isset($_SESSION['giohang']))
			{ 
				for($i=0; $i<count($_SESSION['giohang']); $i++){
					if ($_SESSION['giohang'][$i] == NULL) continue;
					$stt=$i+1;
					$query = mysql_query("select * from sach where ID=".$_SESSION['giohang'][$i]['id']);
					$row = mysql_fetch_array($query);
					$query2=  mysql_query("SELECT * FROM gia_ban WHERE (ID_Sach='".$_SESSION['giohang'][$i]['id']."' and Thoi_gian<now()) order by Thoi_gian DESC limit 1");
					$row2 = mysql_fetch_array($query2);
					$tongtien=$tongtien + ($row2['Gia_ban'] * $_SESSION['giohang'][$i]['soluong']);
					$thanhtien= $row2['Gia_ban'] * $_SESSION['giohang'][$i]['soluong'];
					$query3 = mysql_query("SELECT * FROM Chi_tiet_nhap WHERE (ID_Sach='".$_SESSION['giohang'][$i]['id']."') order by ID DESC limit 1");
					$row3 = mysql_fetch_array($query3);
					$dongianhap=$row3['Gia_nhap'];
					$gia_nhap = $dongianhap * $_SESSION['giohang'][$i]['soluong'];
					$lai=$thanhtien-$gia_nhap;
					$giavon +=$gia_nhap;
					$maxid     = mysql_query("SELECT MAX(ID) as maxid FROM Hoa_don");
            		$row4      = mysql_fetch_array($maxid);
           			$idmax     = $row4["maxid"];
					insert_cthd($_SESSION['giohang'][$i]['id'], $_SESSION['giohang'][$i]['soluong'],$row2['Gia_ban'],$row3['Gia_nhap'],$idmax,$thanhtien,$lai);		
				   include('phpmailer/guidonhang0.php');
				}
				 unset($_SESSION['giohang']);
				
				}
				echo  "<script> alert('Đặt hàng thành công, chúng tôi sẽ liên lạc lại với bạn!'); window.history.back(-1);</script>;";
				
				}else {
					 echo "<script> alert('Cần phải đăng nhập tài khoản!'); window.history.back(-1);</script>";
						}
				}else {
					 echo  "<script> alert('Giỏ hàng trống, mời bạn lựa chọn sản phẩm đưa vào giỏ hàng!'); window.history.back(-1);</script>;";}
		
			}



if (isset($_POST['update'])) {
    foreach ($_POST['qty'] as $key => $value) {
        if (($value == 0) and (is_numeric($value))) {
            unset($_SESSION['giohang'][$key]);
        } elseif (($value > 0) and (is_numeric($value))) {
            $_SESSION['giohang'][$key]['soluong'] = $value;
        }
        
    }
    header('location: cart.php');
}





if (isset($_POST['xoagiohang'])) {
    
    unset($_SESSION['giohang']);
    header('location: cart.php');
    
}

if (isset($_GET['xoa'])) {
    $idx                       = $_GET['xoa'];
    $_SESSION['giohang'][$idx] = NULL;
    header('location: cart.php');
    
}



require('v_cart.php');
	
?>
</body>
</html>